package importPool;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.StringBuffer;


public class SortBuild 
{
	String input_file;
	
	public SortBuild(String input_file) 
		{
			this.input_file =input_file;
		}
//---------------------read input file & return file as string buffer	
	public StringBuffer read_file()
		{	
			try 
			{
				RandomAccessFile file = new RandomAccessFile((new File(input_file)),"r");
				StringBuffer File = new StringBuffer() ;
				String line;
				int i=0;
				while((line=file.readLine())!=null)
				{
					File.append(line+"\n");
					i++;
				}
				file.close();
				return File;
			}
			catch (FileNotFoundException e) 
			{
				System.out.println("FilenotfoundException in ->Read File");
				e.printStackTrace();
			} 
			catch (IOException e) 
			{
				System.out.println("IOException in ->Read File");
				e.printStackTrace();
			}
			return null;
		}
	//-------------------sort String buffer according to y 
	public StringBuffer[] Sort(StringBuffer line[])
	{
		for(int i=0;i<line.length;i++)
		{
			String SubString[] = line[i].toString().split("\t");
			int y = Integer.parseInt(SubString[2]);
			int min =y;
			for(int j=i+1;j<line.length;j++)
			{
				String Sub[] = line[j].toString().split("\t");
				y = Integer.parseInt(Sub[2]);
				if(min>y)
				{
					 StringBuffer temp = line[i];
					 line[i] = line[j];
					 line[j] =  temp;
					 min = y; 
				}
			}
		}
		return line;
	}
	//--------------sort string buffer according to x if y is same----------------
	public StringBuffer[] Sort_x(StringBuffer line[])
	{
		for(int i=0;i<line.length;i++)
		{
			String SubString[] = line[i].toString().split("\t");
			int y = Integer.parseInt(SubString[2]);
			int x = Integer.parseInt(SubString[1]);
			int pre_y =y;
			int min=x;
			for(int j=i+1;j<line.length;j++)
			{
				String Sub[] = line[j].toString().split("\t");
				y = Integer.parseInt(Sub[2]);
				x = Integer.parseInt(Sub[1]);
				if(pre_y==y)
				if(min>x)
				{
					 StringBuffer temp = line[i];
					 line[i] = line[j];
					 line[j] =  temp;
					 min = x; 
				}
			}
		}
		return line;
	}
	//-----------------convert string array into string buffer array
	public StringBuffer[] Convert_String(String sort[])
	{
		StringBuffer temp[] = new StringBuffer[sort.length]; 
		for(int i=0;i<sort.length;i++)
		{
			temp[i] = new StringBuffer(sort[i]);
		}
		return temp;
	}
	
	public static void main(String[] args) 
	{
		SortBuild sb = new SortBuild("/home/wiki/Desktop/ItemList/check.txt");		
		StringBuffer temp1[] =sb.sortfile();
		for(int i=0;i<temp1.length;i++)
		{
			System.out.println("sorted file->"+temp1[i]);
		}
	}
	//------------------return sorted String buffer --------------------
	public StringBuffer[] sortfile()
	{
		return Sort_x( Sort(Convert_String(read_file().toString().split("\n"))));
	}
}
